package com.mapforge.util;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public final class FileUtil {
	/*public static void saveMapCompressed(LinkedMap map, File file)
			throws IOException {
		if (!file.exists())
			file.createNewFile();
		FileOutputStream fout = new FileOutputStream(file);
		BitOutputStream bout = new BitOutputStream(fout);
		
		writeBytes(map.getName(), bout);
		writeBytes(map.getTilesetPath(), bout);
		writeBytes(map.getScript(), bout);
		List<String> layerNames = map.getLayerNames();
		bout.write(layerNames.size());
		bout.write('\n');
		for (int i = 0; i < layerNames.size(); i++) {
			writeBytes(layerNames.get(i), bout);
		}

		Tile[][] tiles = map.getLayers();
		int width = tiles[0].length;
		List<Integer> list;
		int maxVal = Integer.MIN_VALUE;
		int t, i, j, k;
		// Finding max value in tiles
		for (i = 0; i < tiles.length; i++) {
			for (j = 0; j < width; j++) {
				list = tiles[i][j].getLayers();
				for (k = 0; k < list.size(); k++) {
					t = list.get(k);
					if (t > maxVal)
						maxVal = t;
				}
			}
		}
		// Find #bits takes to represent maxValue
		int numBits = 1;
		int val = 1;
		while (val < maxVal) {
			val <<= 1;
			++numBits;
		}
		// Write layer size, then layer bits
		for (i = 0; i < tiles.length; i++) {
			for (j = 0; j < width; j++) {
				list = tiles[i][j].getLayers();
				bout.write(list.size());
				for (k = 0; k < list.size(); k++) {
					bout.writeBits(list.get(k), numBits);
				}
			}
		}
	}*/

	private static void writeBytes(String s, BitOutputStream bout)
			throws IOException {
		byte[] b = (s+"\n").getBytes();
		for (Byte b2 : b)
			bout.write(b2);
	}
	/**
	 * Returns the directory which this file resides in,
	 * or null if the file is a directory.
	 * 
	 * @param f
	 * @return
	 */
	public static String getResidingDirectory(File file){
		String name = file.getAbsolutePath();
		int index = name.lastIndexOf(file.getName());
		System.out.println(name);
		if(index!=-1 && index<name.length()-1){
			return name.substring(0,index);
		}
		else
			return null;
	}
	public static String getFileNameWithoutExt(File f){
		String name = f.getName();
		int index = name.lastIndexOf('.');
		if(index!=-1 && index<name.length()-1){
			return name.substring(0,index);
		}
		else
			return "";
	}
	/**
	 * Returns all the characters proceeding the last '.' in the specified file,
	 * or an empty String should there be no '.' in the file.
	 * @param f specified file to retrieve the extension of
	 * @return the extension of specified file, or an empty String
	 */
	public static String getExt(File f) {
		String name = f.getName();
		int index = name.lastIndexOf('.');
		if (index != -1 && index < name.length() - 1)
			return name.substring(index + 1);
		else
			return "";
	}

	public static void copyFile(File src, File dst) throws IOException {
		// Open or create the output file
		FileInputStream in = new FileInputStream(src);
		FileOutputStream out = new FileOutputStream(dst);
		FileDescriptor fd = out.getFD();

		// Transfer bytes from in to out
		byte[] buffer = new byte[1024];
		int length;
		while ((length = in.read(buffer)) > 0) {
			out.write(buffer, 0, length);
		}

		// Flush the data from the streams and writers into system buffers.
		// The data may or may not be written to disk.
		out.flush();
		out.close();
		in.close();
		// Block until the system buffers have been written to disk.
		// After this method returns, the data is guaranteed to have
		// been written to disk.
		fd.sync();

	}
}